<?php 
$error = $this->request->getParam('error');
if (isset($error) && $error == 1) {
    echo "<script>$.dialog.alert('考试项目,学校,上传文件为必选项',function(){ window.location = '/info/default/index' });</script>";
}
if (isset($error) && $error == 2) {
    echo "<script>$.dialog.alert('当前用户没有导入所选学校的权限',function(){ window.location = '/info/default/index' });</script>";
}
if (isset($error) && $error == 3) {
    echo "<script>$.dialog.alert('该考试项目不包含此次学校',function(){ window.location = '/info/default/index' });</script>";
}
if (isset($error) && $error == 4) {
    echo "<script>$.dialog.alert('excel验证未通过，请根据错误列表修改');</script>";
}
if (isset($error) && $error == 5) {
    echo "<script>$.dialog.alert('数据导入成功',function(){ window.location = '/info/default/index' });</script>";
}
if (isset($error) && $error == 6) {
    echo "<script>$.dialog.alert('文件格式不对,应该是Excel格式',function(){ window.location = '/info/default/index' });</script>";
}
if (isset($error) && $error == 7) {
    echo "<script>$.dialog.alert('Excel数据为空',function(){ window.location = '/info/default/index' });</script>";
}
if (isset($error) && $error == 8) {
    echo "<script>$.dialog.alert('当前用户没有可导入的学校',function(){ window.location = '/info/default/index' });</script>";
}
?>    
<div class="SipeiMain_r fr">
    <h2 class="clearfix fonts_18 height45 fontc_444"><?php echo $this->title; ?></h2>
    <div class="clearfix back_bgf border_3 border_c pr30 pt20 pb12 height32 fontc_666">
        <form class="clearfix pb10" action="<?php echo $this->createUrl('/info/default/import') ?>" enctype="multipart/form-data" method="POST">
            <table>
                <tbody>
                    <tr>
                        <td width="177" align="right" class="pt10 pr2">考试项目：</td>
                        <td width="591" class="pt10">
                            <span class="fl standard_select pt5 pr7 pb5 pl3 border_c border_3">
                                <span class="fl select_shelter">
                                    <select class="fl fontc_666" name="info[exam_id]" id="exam_id" onchange="checkschool(this.value)">
                                        <option value="">请选择</option>
                                        <?php foreach ($date['ztask'] as $value): ?>
                                            <option value="<?php echo $value['exam_id'] ?>"><?php echo $value['exam_term_name'] ?></option>
                                        <?php endforeach; ?>
                                    </select>
                                </span>
                            </span>
                            <div class="clearfix pl30 fr">
                                <input type="button" name="button" onclick="return ajaxFileUpload();" class="b_a_n border_3 back_bgfb w80 height32 fontc_FFF fonts_12 mr10 cursor" value="导入">
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td width="177" align="right" class="pt10 pr2">学校：</td>
                        <td width="591" class="pt10">
                            <span class="fl standard_select pt5 pr7 pb5 pl3 border_c border_3">
                                <span class="fl select_shelter">
                                    <select class="fl fontc_666" name="info[school_id]" id="schools">
                                        <option value="">请选择</option>
                                        <?php foreach ($date['school'] as $value): ?>
                                            <option value="<?php echo $value['school_id'] ?>"><?php echo $value['school_name'] ?></option>
                                        <?php endforeach; ?>
                                    </select>
                                </span>
                            </span>
                        </td>
                    </tr>
                    <tr>
                        <td valign="top" align="right" class="pt10 pr2">模板文件：</td>
                        <td colspan="3" class="pt10"><a href="<?php echo $this->createUrl('/info/default/upload') ?>">高中考试学生数据导入模板.xls</a>&nbsp;&nbsp;<a href="<?php echo $this->createUrl('/info/default/highupload') ?>">初中考试学生数据导入模板.xls</a></td>
                    </tr>
                    <tr>
                        <td valign="top" align="right" class="pt10 pr2">导入文件：</td>
                        <td class="pt10">
                            <input type="button" value="浏览" onclick="path.click()" style="" class="fl b_a_n border_3 back_bgfyg w80 height32 fontc_FFF fonts_12 cursor" />  
                            <input type="text" value="未选择文件" name="upfile" id="upfile" style="border:none;color:#67b94b;" class="fl col2 ml10 height32" />
                            <input type="file" id="path" name="file" style="position:relative;top:-32px;filter:alpha(opacity=0);opacity:0;left:8px;width:30px;height:32px;" onchange="upfile.value=this.value" />	
                        </td>
                    </tr>
                </tbody>
            </table>
        </form>
    </div>
    <div class="clearfix back_bgf border_c mt15 pt20 pb20">
        <table class="height40" width="100%">
            <?php if (!empty($data)): ?>
            <thead class="back_bgf5 fontc_666">
                <tr>
                    <th align="center" class="m_hr_t m_hr_b">序号</th>
                    <th align="center" class="m_hr_t m_hr_b">Excel文件行数</th>
                    <th align="center" class="m_hr_t m_hr_b">错误讯息</th>
                </tr>
            </thead>
            <tbody class="fontc_444">            
            <?php foreach ($data as $k => $v): ?>
                <tr>
                    <td align="center" class="m_hr_b"><?php echo $v['id']; ?></td>
                    <td align="center" class="m_hr_b"><?php echo $v['excel_row']; ?></td>
                    <td align="center" class="m_hr_b"><?php echo $v['info']; ?></td>
                </tr>
            <?php endforeach; ?>       		
            </tbody>
            <?php elseif(!empty($data_student)): ?>
            <thead class="back_bgf5 fontc_666">
                <tr>
                    <th align="center" class="m_hr_t m_hr_b">序号</th>
                    <th align="center" class="m_hr_t m_hr_b">学生姓名</th>
                    <th align="center" class="m_hr_t m_hr_b">班级</th>
                    <th align="center" class="m_hr_t m_hr_b">学校</th>
                    <th width="110" align="center" class="m_hr_t m_hr_b pl30">操作</th>
                </tr>
            </thead>
            <tbody class="fontc_444">
                <?php foreach($data_student as $tValue): ?>
                <tr>
                    <td align="center" class="m_hr_b"><?php echo $tValue['student_id']; ?></td>
                    <td align="center" class="m_hr_b"><?php echo $tValue['student_name']; ?></td>
                    <td align="center" class="m_hr_b"><?php echo $tValue['class_name']; ?></td>
                    <td align="center" class="m_hr_b"><?php echo $tValue['school_name']; ?></td>
                    <td align="center" class="m_hr_b pl30">
                        <a href="javascript:;" class="edit mr10" sid="<?php echo $tValue['student_id']; ?>" eid="<?php echo $tValue['exam_id']; ?>" title="编辑"><img src="/themes/default/images/Sipei/edit.png" /></a>
                        <a href="javascript:;" class="del" sid="<?php echo $tValue['student_id']; ?>" eid="<?php echo $tValue['exam_id']; ?>" title="删除"><img src="/themes/default/images/Sipei/delate.png" /></a>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
            <?php endif; ?>
        </table>
        <?php $this->renderPartial('//partial/partial_pager', array('pages' => $pages, "url" => $pageUrl)); ?>
    </div>
</div>
<script>
    function checkschool(val){
        $.post('/info/default/ajaxvalue', {id: val}, function(result) {
            var dataObj=eval("("+result+")");//转换为json对象
            var schoolstr = '';
            $.each(dataObj,function(i,item){
                    schoolstr += "<option value='" + item.school_id + "'>" + item.school_name + "</option>"
            });
            $("#schools").html(schoolstr);
        });
    }
    
    //编辑
    $(".edit").click(function(){
        var sid = $(this).attr('sid');
        var eid = $(this).attr('eid');
        api = $.dialog({
            id: 'testID2',
            lock: true,
            padding:0,
            fixed: true,
            title:"编辑",
            width:480,
            height:260,
            max: false,
            min: false
        });

        $.get('/info/default/update',{sid:sid,eid:eid,time:new Date()},function(html){
            api.content(html);
        });
    });
    
    //删除
    $(".del").click(function(){
        var sid = $(this).attr('sid');
        var eid = $(this).attr('eid');
        var that = $(this);
        $.dialog.confirm('确定删除吗',function(){
            $.get('/info/default/delete',{sid:sid,eid:eid},function(html){
                if(html==1){
                    that.parents('tr').remove();
                }
                api.content(html);
            });
        });
    });
</script>



<script type="text/javascript">
    var verifycount = 0;
    var imCount = 0;
	function ajaxFileUpload()
	{
        imCount = verifycount = 0;
        $.dialog.tips('数据上传中...',3).size(200,50).unlock();
        var exam_id = $("#exam_id").val();
        var school_id = $("#schools").val();
        if(!exam_id || !school_id){
            $.dialog.alert('考试项目,导入文件为必选项');
        }
		$.ajaxFileUpload
		(
			{
				url:'/info/default/uploadexcel',
				secureuri:false,
				fileElementId:'path',
				dataType: 'json',
				data:{exam_id:exam_id, school_id:school_id,isajax:1},
				success: function (data, status)
				{
                    if(data.code==1){
                        $.dialog.tips('上传完成开始验证数据...',3).size(200,50).unlock();
                        verify(data.ext);   
                    }else if(data.code>200 && data.code<300){
                        $.dialog.alert(data.msg);
                    }else{
                        $.dialog.alert("文件上传失败");
                    }
				},
				error: function (data, status, e)
				{
					$.dialog.alert("文件上传失败");
				}
			}
		)
		return false;
	}
    function verify(ext){
        $.ajax({
            type: 'POST',
            url:'/info/default/verifyexcel',
            data: "f="+ext.f+"&sc="+ext.sc+"&st="+ext.st+"&eid="+ext.eid+"&sid="+ext.sid+"&salt="+ext.salt+"&he="+ext.he+"&bt="+ext.bt+"&ct="+ext.ct,
            dataType: 'json',
            success: function(data){
                if(data.code==1){
                    if(data.ext.he==1){
                        window.location.href="/info/default/index/error/4/exam_id/"+data.ext.eid;
                    }else{
                        $.dialog.tips('验证完成开始导入...',3).size(200,50).unlock();
                        imp(data.ext);
                    }
                }else if(data.code==2){
                    verifycount += data.ext.ct;
                    $.dialog.tips('已经验证('+verifycount+')条...',3).size(200,50).unlock();
                    verify(data.ext);
                }else if(data.code==200 && data.code<300){
                    $.dialog.alert(data.msg);
                }else{
                    $.dialog.alert('导入失败');
                }
            }
       });
    }
    function imp(ext){
        $.ajax({
            type: 'POST',
            url:'/info/default/saveexcel',
            data: "f="+ext.f+"&eid="+ext.eid+"&sid="+ext.sid+"&salt="+ext.salt+"&he="+ext.he+"&bt="+ext.bt+"&ct="+ext.ct,
            dataType: 'json',
            success: function(data){
                if(data.code==1){
                    $.dialog.alert('导入完成');
                }else if(data.code==2){
                    imCount += parseInt(data.ext.ct);
                    $.dialog.tips('已经导入'+imCount+'条...',3).size(200,50).unlock();
                    imp(data.ext);
                }else{
                    $.dialog.alert('导入失败');
                }
            }
       });
    }
	</script>